const jumpLogin=()=>{
        window.location.href='./login.html'
    }
const li1s=document.querySelectorAll('.nan li')
const line1=document.querySelector('.line1')

const div1=document.querySelector('.content-sort')
const div2=document.querySelector('.rcul')
const changeContentMan=(e=0)=>{
    div1.innerHTML=''
    div2.innerHTML=''
    fetch(`https://www.zongheng.com/api2/catefine/storeSearchConf?bookType=${e}`).then(res=>res.json()).then(res1=>{
        let total=''
        res1.result.categoryList.list.forEach((item,index)=>{
            if(index==0){
                total+=`
                    <li data-sort="0" onclick="getValue1(event)" class="li2Active">${item.name}</li>
                `
            }else{
                    total+=`
                        <li data-sort="${item.paramValue}" onclick="getValue1(event)">${item.name}</li>
                    `
            }
            
        })
        div1.innerHTML+=`
            <div class="everyContent">
                <p class="csT">作品分类</p>
                <ul class="csul csul1">
                    ${total}
                </ul>
            </div>
        `
        res1.result.filtrateTypeList.forEach((item,index)=>{
            if(index==0){
                let allList=''
                item.filtrateOptionList.forEach((items, index1) => {
                    if (index1 == 0) {
                        allList += `
                        <li data-sort="${items.paramValue}" onclick="getValue2(event)" class="li2Active">${items.name}</li>
                    `
                    } else {
                        allList += `
                        <li data-sort="${items.paramValue}" onclick="getValue2(event)">${items.name}</li>
                    `
                    }
                })
                div1.innerHTML+=`
                    <div class="everyContent">
                        <p class="csT">${item.name}</p>
                        <ul class="csul csul2">
                            ${allList}
                        </ul>
                    </div>
                `
            }else if(index==1){
                let allList=''
                item.filtrateOptionList.forEach((items, index1) => {
                    if (index1 == 0) {
                        allList += `
                        <li data-sort="${items.paramValue}" onclick="getValue3(event)" class="li2Active">${items.name}</li>
                    `
                    } else {
                        allList += `
                        <li data-sort="${items.paramValue}" onclick="getValue3(event)">${items.name}</li>
                    `
                    }
                })
                div1.innerHTML+=`
                    <div class="everyContent">
                        <p class="csT">${item.name}</p>
                        <ul class="csul csul3">
                            ${allList}
                        </ul>
                    </div>
                `
            }else{
                let allList=''
                item.filtrateOptionList.forEach((items, index1) => {
                    if (index1 == 0) {
                        allList += `
                        <li data-sort="${items.paramValue}" onclick="getValue4(event)" class="li2Active">${items.name}</li>
                    `
                    } else {
                        allList += `
                        <li data-sort="${items.paramValue}" onclick="getValue4(event)">${items.name}</li>
                    `
                    }
                })
                 div1.innerHTML+=`
                    <div class="everyContent">
                        <p class="csT">${item.name}</p>
                        <ul class="csul csul4">
                            ${allList}
                        </ul>
                    </div>
                `
            }
        })
        res1.result.sortOptionList.forEach((item,index)=>{
            if(index==0){
                div2.innerHTML+=`
                     <li onclick="getValue5(event)" data-sort="${item.paramValue}" class="active3">${item.name}</li> 
                `
            }else{
                div2.innerHTML+=`
                     <li onclick="getValue5(event)" data-sort="${item.paramValue}">${item.name}</li> 
                `
            }
        })
    })
}
changeContentMan()
const currentFilters = {
    worksTypes: 0,
    bookType: 0,
    subWorksTypes: 0,
    totalWord: 0,
    serialStatus: 9,
    vip: 9,
    categoryId: 0,
    categoryPid: 0,
    naodongFilter: 0,
    order:'weekOrder',
    pageNum:1,
    pageSize:20
};
const ul2=document.querySelector('.bookList')

const getList=(worksTypes=0,bookType=0,subWorksTypes=0,totalWord=0,serialStatus=9,vip=9,categoryId=0,categoryPid=0,naodongFilter=0,order='weekOrder',pageNum=1,pageSize=20)=>{
    const queryParams = new URLSearchParams({
        worksTypes,
        bookType,
        subWorksTypes,
        totalWord,
        serialStatus,
        vip,
        categoryId,
        categoryPid,
        naodongFilter,
        order,
        pageNum,
        pageSize
    })
    fetch(`https://www.zongheng.com/api2/catefine/storeSearch?${queryParams}`,{
        method:'POST'
    }).then(res=>res.json()).then(res1=>{
        console.log(res1)
        res1.result.bookList.forEach(item=>{
            ul2.innerHTML+=`
                <li>
                    <div class="left-imgbox">
                        <img src="${item.picUrl}" alt="">
                    </div>
                    <div class="right-txt">
                        <p class="title">${item.name}</p>
                        <p class="sort"><span>${item.authorName}</span>·<span>${item.categoryName}</span>·连载·${Math.floor(item.totalWords/10000)}万字</p>
                        <p class="intro">
                            ${item.description}
                        </p>
                        <div class="bottom-txt">
                            <p class="zhanjie">
                                ${item.theNewChapter}
                            </p>
                            <p class="time">${item.orderColumnInfo}</p>
                        </div>
                    </div>
                </li>
            `
        })
        
    }).catch(err=>{
        console.log(err)
    })
}
getList()

const scrollToTopBtn=document.querySelector('.returnTop')
scrollToTopBtn.addEventListener('click', function() {
            window.scrollTo({
                top: 0,
                behavior: 'smooth'
            })
        })


function handleTabClick(paramName, value, event, groupContainerSelector, itemSelector, activeClass) {
    currentFilters[paramName] = value;
    
    
    if (groupContainerSelector && activeClass) {
        updateTabUI(groupContainerSelector, itemSelector, activeClass, event);
    }
    
    // 发送请求
    sendFilterRequest();
}

// 简化handleTabClick函数
function handleTabClick(paramName, value, event, containerSelector, activeClass) {
    currentFilters[paramName] = value;
    
    updateTabUI(containerSelector, activeClass, event);
    
    sendFilterRequest();
}


function updateTabUI(containerSelector, activeClass, event) {
    
    const container = event.target.closest(containerSelector);
    
    if (container) {
        const items = container.querySelectorAll('li');
        items.forEach(item => {
            item.classList.remove(activeClass);
        });
        event.target.classList.add(activeClass);
    }
}

const changeSex = (event) => {
    const value = parseInt(event.target.dataset.sort);
    handleTabClick('bookType', value, event, '.nan', 'liActive');
    line1.style.left = value === 0 ? '0' : '100px';
    changeContentMan(value);
};

const getValue1 = (event) => {
    const value = parseInt(event.target.dataset.sort);
    handleTabClick('categoryPid', value, event, '.csul1', 'li2Active');
};

const getValue2 = (event) => {
    const value = parseInt(event.target.dataset.sort);
    handleTabClick('totalWord', value, event, '.csul2', 'li2Active');
};

const getValue3 = (event) => {
    const value = parseInt(event.target.dataset.sort);
    handleTabClick('serialStatus', value, event, '.csul3', 'li2Active');
};

const getValue4 = (event) => {
    const value = parseInt(event.target.dataset.sort);
    handleTabClick('vip', value, event, '.csul4', 'li2Active');
};

const getValue5 = (event) => {
    const value = event.target.dataset.sort;
    handleTabClick('order', value, event, '.rcul', 'active3');
};

// 发送请求
function sendFilterRequest() {
    ul2.innerHTML = '';
    getList(
        currentFilters.worksTypes,
        currentFilters.bookType,
        currentFilters.subWorksTypes,
        currentFilters.totalWord,
        currentFilters.serialStatus,
        currentFilters.vip,
        currentFilters.categoryId,
        currentFilters.categoryPid,
        currentFilters.naodongFilter,
        currentFilters.order,
        currentFilters.pageNum,
        currentFilters.pageSize
    );
}
const newpage = () => {
    layui.use(function () {
        var laypage = layui.laypage;
        laypage.render({
            elem: 'demo-laypage-all',
            // 数据总数，从后端得到
            layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
            count: 10000,

            jump: function (obj, first) {
                currentFilters.pageNum=obj.curr
                currentFilters.pageSize=obj.limit
                console.log(obj.curr); 
                sendFilterRequest()
                // 得到当前页，以便向服务端请求对应页的数据。
                console.log(obj.limit); // 得到每页显示的条数

                // 首次不执行
                if (!first) {
                    // do something
                }
            }
        })
    })
}
newpage()
const setNew=()=>{
    location.href=`${event.target.dataset.index}.html`
}

jump=()=>{
    window.location.href='index.html'
}

to=()=>{
    console.log(9999)
     const inputElement = document.querySelector('.search-box input');
    const searchText = inputElement.value.trim();
        console.log(searchText);
        inputElement.value = '';
        window.location.href = 'shearh.html?searchText=' + searchText;
}

diji=()=>{
    window.location.href='naodongxingqiu_author.html'
}
ju=()=>{
    window.location.href='detail.html'
}